package com.rh.controller;

import com.rh.Enums;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.Role;
import com.rh.excrptions.DoException;
import com.rh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@CrossOrigin
@ResponseBody
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping(value = "/add_role",method = RequestMethod.POST)
    public Result addRole(Role role){
        try{
            System.out.println("===>"+role);
            //int i=1/0;
            roleService.addMethod(role);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.ADD_ERROR);
        }
    }

    /**
     * 删除
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/delete_role",method = RequestMethod.GET)
    public Result deleteRole(Integer roleId){
        try{
            //int i=1/0;
            roleService.deleteMethod(roleId);
            Result result = new Result();
            return  result;
        }catch (Exception e){
            throw new DoException(Enums.DELETE_ERROR);
        }
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @RequestMapping(value = "/update_role",method = RequestMethod.POST)
    public Result updateRole(Role role){
        try{
            //int i=1/0;
            roleService.updateMethod(role);
            Result result = new Result();
            return  result;
        }catch (Exception e){
            throw new DoException(Enums.UPDATE_ERROR);
        }
    }
    /**
     * 分页
     */
    @RequestMapping("/getRolePage")
    public Result getRolePage(Integer pageNo,Integer pageSize) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 2;
            }
            Result result = new Result();
            Page methodPage = roleService.getMethodPage(pageNo, pageSize);
            result.setData(methodPage);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
    /**
     * 根据id查询所有
     */
    @RequestMapping("/getRoleByRoleId")
    public Result getRoleAllById(Integer roleId) {
        try {
            Result result = new Result();
            Role methodAllById = roleService.getMethodAllById(roleId);
            result.setData(methodAllById);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
}
